package com.woniuxy.yoga.progress.web.controller;


import cn.hutool.core.bean.BeanUtil;
import com.woniuxy.yoga.commons.core.model.Result;
import com.woniuxy.yoga.commons.web.log.YogaLog;
import com.woniuxy.yoga.progress.dao.model.Subcard;
import com.woniuxy.yoga.progress.model.form.CreateSubCardForm;
import com.woniuxy.yoga.progress.model.param.CreateSubCardParam;
import com.woniuxy.yoga.progress.service.SubcardService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import javax.validation.constraints.NotNull;
import java.util.List;

/**
 * <p>
 * 次卡流程表 前端控制器
 * </p>
 *
 * @author 韦用文,刘存益,郎腾霄
 * @since 2023年11月28日
 */
@RestController
@RequestMapping("/subcard")
@Api(tags = "次卡管理")
@Validated
public class SubcardController {

    @Autowired
    private SubcardService subcardService;

    /**
     * 创建次卡进度表
     * @param form 创建次卡进度表参数
     * @return String
     */
    @PostMapping("/createSubCard")
    @ApiOperation("创建次卡进度表")
    @YogaLog("创建次卡进度表")
    public Result<?> createSubCard(@RequestBody @Valid CreateSubCardForm form){
        subcardService.createSubCard(BeanUtil.toBean(form, CreateSubCardParam.class));
        return Result.getResult().setMessage("创建成功");
    }

    /**
     * 完成次卡
     * @param subcardId 次卡进度卡id
     * @return String
     */
    @GetMapping("/finishSubcard/{subcardId}")
    @ApiOperation("完成次卡")
    @YogaLog("完成次卡")
    public Result<String> finishSubcard(@PathVariable("subcardId") @Valid @NotNull Integer subcardId){
        subcardService.finishSubcard(subcardId);
        return Result.getResult(String.class).setMessage("操作成功");
    }

    /**
     * 次卡进度次数加一
     * @param subcardId 次卡id
     * @return string
     */
    @GetMapping("/addSubCardCount/{subcardId}")
    @ApiOperation("次卡进度次数加一")
    @YogaLog("次卡进度次数加一")
    public Result<String> addSubCardCount(@PathVariable("subcardId") @Valid @NotNull Integer subcardId) throws Exception {
        subcardService.addSubCardCount(subcardId);
        return Result.getResult(String.class).setMessage("操作成功");
    }

    /**
     * 次卡进度次数减一
     * @param subcardId 次卡id
     * @return string
     */
    @GetMapping("/reduceSubCardCount/{subcardId}")
    @ApiOperation("次卡进度次数减一")
    @YogaLog("次卡进度次数减一")
    public Result<String> reduceSubCardCount(@PathVariable("subcardId") @Valid @NotNull Integer subcardId) throws Exception{
        subcardService.reduceSubCardCount(subcardId);
        return Result.getResult(String.class).setMessage("操作成功");
    }

    /**
     * 查询未完成次卡
     * @param userId userId
     * @return Subcard
     */
    @GetMapping("/getUnfinishSub/{userId}")
    @ApiOperation("查询未完成次卡")
    @YogaLog("查询未完成次卡")
    public Result<List<Subcard>> getUnfinishSub(@PathVariable("userId") @Valid @NotNull Integer userId){
        List<Subcard> subcards = subcardService.getUnfinishSub(userId);
        return Result.getListResult(Subcard.class).setMessage("查询成功").setData(subcards);
    }
}

